<?php
/**
 * 处理控制器
 */
namespace app\manage\controller\goods;

use app\manage\controller\Base;
use app\common\query\MallGoods;
use app\common\query\Plugin;

class Handle extends Base
{
	private $params;
    public function __construct(){
        parent::__construct();
        //token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    /**
     * [add 添加商品]
     */
	public function add(){
    	$goodsData = $this->params;
	    $goodsData['create_time'] =time();
    	$result = $this->validate($goodsData, 'MallGoods.manage_add');
    	if(true !== $result) {
    	    $this->error($result);
        }
        $result = model('MallGoods')->addOneData($goodsData);
        if(false === $result){
            $this->error($query->getError());
        }
        if(true===$result){
            $this->success('添加成功');
        }
        $this->success('添加成功');
    }

    /**
     * [uploads 上传文件]
     * @return [type] [description]
     */
    public function uploads(){
        $file = request()->file('image');
        if($file){
            $info = $file->validate(['size'=>1567800,'ext'=>'bmp,jpg,jpge,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $file_image = '/uploads/'.$info->getSaveName();
                return json(['errno'=>0,'data'=>[$file_image]]);
            }else{
                // 上传失败获取错误信息
                return json($file->getError());
            }
        }
    }

    /**
     * 删除商品
     * @return [type] [description]
     */
    public function del(){
    	$params = $this->params;
        $id = isset($params['id'])?intval($params['id']):0;
        if(!$id){
            $this->error('请选择删除对象');
        }
        if(false===model('MallGoods')->deleteOneData($id)){
             $this->error($query->getError());
        }else{
            $this->success('删除成功');
        }
    }

    /**
     * 编辑商品
     * @return [type] [description]
     */
    public function edit(){
    	$goodsData = $this->params;
    	$id = $goodsData['id'];
    	unset($goodsData['id']);
        $validate = $this->validate($goodsData, 'MallGoods.manage_edit');
        if(true!==$validate){
            $this->error($validate);
        }
        if(model('MallGoods')->saveOneData($goodsData,$id)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 商品是否推荐,是否上架
     * @return [type] [description]
     */
    public function isTj(){
        $params = $this->params;
        if(empty($params['id']))return json(['code'=>0,'msg'=>'数据传递错误']);
        if(isset($params['is_tj']))$data = ['is_tj'=>$params['is_tj']];
        if(isset($params['status']))$data = ['status'=>$params['status']];
        model('MallGoods')->saveOneData($data,$params['id']);
        return json(['code'=>1,'msg'=>'操作成功']);
    }

}